package hw20170814.q2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import org.junit.Test;

public class CreateTable {
	private Connection conn = null;
	private Statement stm = null;
	private ResultSet rs = null;
	private Dbutil db = new Dbutil();

	public void create(String name) {
		String sql = "CREATE TABLE li_t_" + name + "("
				+ " id INT NOT NULL AUTO_INCREMENT," 
				+ " typeid INT,"
				+ " questionid INT,"
				+ " answer VARCHAR(20),"
				+ " PRIMARY KEY (id)"
				+ " )ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		try {
			conn = db.getConnection();
			stm = conn.createStatement();
			stm.execute(sql);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.closeAll();
		}
	}

	public void delete(String name) {
		String sql = "DROP TABLE IF EXISTS li_t_" + name + ";";
		try {
			conn = db.getConnection();
			stm = conn.createStatement();
			stm.execute(sql);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.closeAll();
		}
	}

	public void dbinsert(String name) {
		String sql = "insert into li_t_" + name + "(typeid,questionid,answer) value("+ "1," + "1," + "'对象'" + ")";

		try {
			conn = db.getConnection();
			stm = conn.createStatement();
			stm.execute(sql);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			db.closeAll();
		}
	}

	public static void main(String[] args) {
		CreateTable ct = new CreateTable();

		ct.delete("ljp");
		ct.create("ljp");
		ct.dbinsert("ljp");
	}
}
